package com.macro.mall.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.SmsOperateLogQuery;
import com.macro.mall.mapper.SmsOperateLogMapper;
import com.macro.mall.model.SmsOperateLog;
import com.macro.mall.model.SmsOperateLogExample;
import com.macro.mall.service.SmsOperateLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 郎
 */
@Service
@AllArgsConstructor
public class SmsOperateLogServiceImpl implements SmsOperateLogService {
    private final SmsOperateLogMapper smsOperateLogMapper;

    /**
     * 分页查询日志
     *
     * @param query
     * @param pageNum
     * @param pageSize
     */
    @Override
    public List<SmsOperateLog> list(SmsOperateLogQuery query, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        SmsOperateLogExample example = new SmsOperateLogExample();
        SmsOperateLogExample.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotBlank(query.getUsername())) {
            criteria.andUsernameLike("%" + query.getUsername() + "%");
        }
        if (StrUtil.isNotBlank(query.getDescription())) {
            criteria.andDescriptionLike("%" + query.getDescription() + "%");
        }
        if (StrUtil.isNotBlank(query.getService())) {
            criteria.andServiceEqualTo(query.getService());
        }
        if (StrUtil.isNotBlank(query.getMethod())) {
            criteria.andMethodEqualTo(query.getMethod());
        }
        if (ObjUtil.isNotEmpty(query.getOperateStartTime())) {
            criteria.andOperateTimeGreaterThanOrEqualTo(query.getOperateStartTime());
        }
        if (ObjUtil.isNotEmpty(query.getOperateEndTime())) {
            criteria.andOperateTimeLessThanOrEqualTo(query.getOperateEndTime());
        }
        example.setOrderByClause("operate_time desc");
        if (query.isHasResult()) {
            return smsOperateLogMapper.selectByExampleWithBLOBs(example);
        }
        return smsOperateLogMapper.selectByExample(example);
    }

    @Override
    public SmsOperateLog detail(Long id) {
        return smsOperateLogMapper.selectByPrimaryKey(id);
    }
}
